import {routes} from '@/router'

export default {
    state: {
        menus: []
    },
    actions: {
        generateMenu({commit}) {
            let menus = [];

            routes.forEach((item) => {
                if (item.meta && item.meta.isMenu && menus.length === 0) {
                    menus = generateMenuRecursive(item.children);
                }
            });
            console.info(menus);
            commit('setMenus', menus);
        }
    },
    mutations: {
        setMenus(state, menus) {
            state.menus = menus;
        }
    },
    getters: {
        menus: s => s.menus
    }
}

const generateMenuRecursive = (items = []) => {
    let menus = [];
    items.forEach((item) => {
        if (item.meta && item.meta.isMenu) {
            if (item.children && item.children.length) {
                item.children = generateMenuRecursive(item.children);
            }
            menus.push({
                name: item.name,
                ...item.meta,
                children: item.children
            });
        }
    });
    return menus;
};